﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.CMS.Providers;

namespace MethodWorx.CMS.Plugins.ViewManagement
{
    public class StaticViewService
    {
        private PluginService service;
        private IPlugin plugin;

        public StaticViewService(ICmsDataProvider provider)
        {
            this.service = new PluginService(provider);
            var repository =  new PluginRepository(provider);
            this.plugin = repository.All().First(p => p.Id == ViewManagementExtension.GetPluginId());
        }

        public void Create(StaticView staticView)
        {
            this.service.SetUserData(this.plugin, null, "StaticView", staticView, false);
        }

        public IEnumerable<StaticView> GetAll()
        {
            return this.service.GetAllUserData<StaticView>(this.plugin, null, "StaticView").Select(ud =>
                                                                                                       {
                                                                                                           ud.UserData.
                                                                                                               Id =
                                                                                                               ud.Id;
                                                                                                           return
                                                                                                               ud.
                                                                                                                   UserData;
                                                                                                       });
        }

        public StaticView GetById(Guid id)
        {
            return this.service.GetUserData<StaticView>(id);
        }

        public void Save(Guid id, StaticView staticView)
        {
            this.service.SetUserData(id, staticView);
        }
    }
}
